<?php

namespace backend\models\service;

use Yii;
use yii\data\Sort;
use yii\data\ActiveDataProvider;
use backend\models\odm\Novel;

class NovelService extends Novel {

    private $_query;

    public function init() {
        parent::init();
        $this->_query = static::find();
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['novel_chapter', 'novel_type', 'novel_status', 'novel_charge_id', 'novel_op_status', 'novel_main_category', 'novel_category', 'novel_word_total'], 'integer'],
            [['novel_cover'], 'required'],
            [['novel_create_time', 'novel_update_time'], 'safe'],
            [['novel_author', 'novel_book_number'], 'string', 'max' => 100],
            [['novel_title', 'novel_pinyin', 'novel_cover', 'novel_introduction'], 'string', 'max' => 200],
        ];
    }

    /**
     * Creates data provider instance with search query applied
      `novel_create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '创建时间',
      `novel_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
      `novel_word_total` bigint(20) NOT NULL DEFAULT '0' COMMENT '小说字数',
      `novel_book_number` varchar(100) NOT NULL DEFAULT '' COMMENT '小说编号',
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function getQuerySearch($params) {
        $this->load($params);
//        $this->_query->join('LEFT JOIN','xs_novel_channel_relation','xs_novel.novel_id=xs_novel_channel_relation.r_novel_id');
        $this->_query->andFilterWhere([
//            'id' => $this->novel_id,
            'novel_author' => $this->novel_author,
//            'novel_title' => $this->novel_title,
            'novel_pinyin' => $this->novel_pinyin,
            'novel_chapter' => $this->novel_chapter,
            'novel_type' => $this->novel_type,
            'novel_charge_id' => $this->novel_charge_id,
            'novel_op_status' => $this->novel_op_status,
            'novel_main_category' => $this->novel_main_category,
            'novel_category' => $this->novel_category,
//            'cover' => $this->novel_cover,
//            'introduction' => $this->novel_introduction,
//            'word_total' => $this->novel_word_total,
            'novel_book_number' => $this->novel_book_number,
        ]);
        $this->_query->andFilterWhere(['like', 'novel_title', $this->novel_title]);
        $this->_query->andFilterWhere(['like', 'novel_book_number', $this->novel_book_number]);
        $this->time($this->_query, [
            'novel_create_time',
        ]);
        return $this;
    }

    public function getList() {
        $query = $this
                ->getQuerySearch(Yii::$app->request->queryParams)
                ->getQuery();

        return new ActiveDataProvider([
            'query' => $query,
        ]);
    }

    /**
     *
     * 时间的搜索
     *
     * @param $query , array $colmns
     *
     * @return $query
     * */
    private function time(&$query, $colmns) {
        foreach ($colmns as $key => $value) {
            if ($this->$value != '') {
                $query->andWhere(['between', $value, strtotime($this->$value), strtotime($this->$value) + 86400]);
            } else {
                $this->$value = null;
            }
        }
        return $query;
    }

    public function getQuery() {
        return $this->_query;
    }
    
    public function upChargeStatus($novel_id,$status) {
        if(is_array($novel_id)){
            $ids = implode(',', $novel_id);
            $sql = "update  xs_novel set novel_charge_id = " .$status. " where novel_id in (" . $ids .")";
        }else{
            $sql = "update  xs_novel set novel_charge_id = " .$status. " where novel_id = " . $novel_id;
        }
        $res = Yii::$app->db->createCommand($sql)->execute();
        return $res;
    }
    
    /**
     * 获取小说信息
     * @param type $novel_id
     * @param type $status
     * @return type
     */
    public function getNovelInfo($novel_id) {
        $sql = "select * from xs_novel where novel_id = " . $novel_id;
        $info = Yii::$app->db->createCommand($sql)->queryOne();
        return $info;
    }
    
    
    public function upCate($id,$cid,$mid){
        if(is_array($id)){
            $ids = implode(',', $id);
            $sql = "update  xs_novel set novel_category = " .$cid. " ,novel_main_category = ".$mid. " where novel_id in ( " . $ids . ")";
        }else{
            $sql = "update  xs_novel set novel_category = " .$cid. " ,novel_main_category = ".$mid. " where novel_id = " . $id;
        }
        $res = Yii::$app->db->createCommand($sql)->execute();
        return $res;
    }
    
    public function bathUp($ids){
        foreach ($ids as $id ){
            $sql = "update  xs_novel set novel_op_status = 1  where novel_id = " . $id;
            $res = Yii::$app->db->createCommand($sql)->execute();
        }
        return $res;
    }
    
     public function bathDown($ids){
        foreach ($ids as $id ){
            $sql = "update  xs_novel set novel_op_status = 0  where novel_id = " . $id;
            $res = Yii::$app->db->createCommand($sql)->execute();
        }
        return $res;
    }
    
    
}
